if(MINGW)
  set(_3RD_PARTY_ROOT ${CMAKE_SOURCE_DIR}/ThirdPartyLibrary/Mingw-w64-gcc13.2)
  set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Resource/cmake;${CMAKE_MODULE_PATH}")
  set(CMAKE_MODULE_PATH "${_3RD_PARTY_ROOT}/PacmanInstalled/lib/cmake;${CMAKE_MODULE_PATH}")
else()
  set(_3RD_PARTY_ROOT ${CMAKE_SOURCE_DIR}/ThirdPartyLibrary/Ubuntu22-gcc11.4)
  set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Resource/cmake;${CMAKE_MODULE_PATH}")  
endif()

list(APPEND CMAKE_PREFIX_PATH ${_3RD_PARTY_ROOT}) # 添加第三方库搜索路径

# 导入highfive
macro(import_highfive)

  if(NOT HighFive_FOUND)
    list(APPEND CMAKE_PREFIX_PATH
        ${_3RD_PARTY_ROOT}/HighFive-2.9.0/share/HighFive/CMake)
        
    # 当并行版本时，使用支持并行的HDF5库
    if (SG_USE_MPI)
      set(HIGHFIVE_PARALLEL_HDF5 ON)
      set(HDF5_DIR ${_3RD_PARTY_ROOT}/PHDF5-1.14.5/cmake)
    else()
      set(HDF5_DIR ${_3RD_PARTY_ROOT}/HDF5-1.14.5/cmake)
    endif()  
    # message(STATUS "Using HDF5: ${HDF5_DIR}")

    find_package(HDF5 QUIET REQUIRED)
    find_package(HighFive QUIET REQUIRED)
  endif()

  target_link_libraries(${PROJECT_NAME} PRIVATE HighFive)
  target_link_libraries(${PROJECT_NAME} PRIVATE HDF5::HDF5)

endmacro()

# 导入json
macro(import_json)
  list(APPEND CMAKE_PREFIX_PATH
       ${_3RD_PARTY_ROOT}/nlohmann-json/share/nlohmann_json)
  find_package(nlohmann_json QUIET REQUIRED)
  link_libraries(nlohmann_json::nlohmann_json)
endmacro()

# 导入magic_enum
macro(import_magicEnum)
  list(APPEND CMAKE_PREFIX_PATH
       ${_3RD_PARTY_ROOT}/magic_enum/share/cmake/magic_enum)
  target_compile_definitions(${PROJECT_NAME} PRIVATE MAGIC_ENUM_RANGE_MIN=-1024)
  target_compile_definitions(${PROJECT_NAME} PRIVATE MAGIC_ENUM_RANGE_MAX=1024)
  # 链接库，将枚举转为字符串
  find_package(magic_enum CONFIG REQUIRED)
  target_link_libraries(${PROJECT_NAME} PRIVATE magic_enum::magic_enum)
endmacro()

# 导入Boost库
macro(import_boost)
  # 3.30以后不对Boost库支持
  if(COMMAND CMAKE_POLICY)
    if(POLICY CMP0167)
      cmake_policy(SET CMP0167 NEW)
    endif()
  endif(COMMAND CMAKE_POLICY)

  # 设置BOOST生成二进制文件路径
  if(MINGW)
    list(APPEND CMAKE_PREFIX_PATH ${_3RD_PARTY_ROOT}/boost_1_85/lib/cmake/Boost-1.85.0)
  endif()

  find_package(Boost QUIET REQUIRED)
  link_libraries(Boost::boost)

endmacro()

# 导入Eigen3
macro(import_eigen3)
  list(APPEND CMAKE_PREFIX_PATH ${_3RD_PARTY_ROOT}/Eigen-3.4/cmake)
  find_package(Eigen3 QUIET REQUIRED)
  link_libraries(Eigen3::Eigen)
endmacro()

# 导入GTest
macro(import_gtest)
  if(MINGW)
    list(APPEND CMAKE_PREFIX_PATH ${_3RD_PARTY_ROOT}/gtest-1.14/lib/cmake/GTest)
  endif()
  find_package(GTest QUIET REQUIRED)
  target_link_libraries(${PROJECT_NAME} PRIVATE ${GTEST_BOTH_LIBRARIES})
endmacro()

# 导入Metis
macro(import_metis pname)
  target_link_libraries(${pname} PRIVATE metis)
  target_link_directories(${pname} PRIVATE
                          ${_3RD_PARTY_ROOT}/METIS-5.1.0/lib)
  target_include_directories(${pname}
                             PRIVATE ${_3RD_PARTY_ROOT}/METIS-5.1.0/include)
endmacro()

# 导入ParMetis
macro(import_ParMetis pname)
  if(MINGW)
    find_package(ParMetis QUIET REQUIRED)
    target_link_libraries(${pname} PRIVATE ParMetis::ParMetis)
    target_link_directories(${pname} PRIVATE
      ${_3RD_PARTY_ROOT}/PacmanInstalled/lib)
  else()
    target_link_directories(${pname} PRIVATE ${_3RD_PARTY_ROOT}/parmetis-4.0.3/lib)
    target_include_directories(${pname} PRIVATE ${_3RD_PARTY_ROOT}/parmetis-4.0.3/include)
    target_link_libraries(${pname} PRIVATE parmetis mpi)
  endif()
endmacro()

# 导入libbacktrace
macro(import_libbacktrace)
  if(MINGW)
    target_link_libraries(${PROJECT_NAME} PRIVATE backtrace)
    target_link_directories(${PROJECT_NAME} PRIVATE
                            ${_3RD_PARTY_ROOT}/libbacktrace/lib)
    target_include_directories(${PROJECT_NAME}
                               PRIVATE ${_3RD_PARTY_ROOT}/libbacktrace/include)
  endif()
endmacro()

macro(import_licensecc)
  if(SG_LICENSE_CHECK)
    add_definitions(-DSG_LICENSE_CHECK)
    if(UNIX)
      list(APPEND CMAKE_PREFIX_PATH ${_3RD_PARTY_ROOT}/licensecc/lib/cmake/licensecc)
    elseif(MINGW)
      list(APPEND CMAKE_PREFIX_PATH ${_3RD_PARTY_ROOT}/licensecc/cmake/licensecc)
    else()
      message(FATAL_ERROR "License check is not supported on currnet platform .")
    endif()
    
    find_package(licensecc 2.0.0 REQUIRED)
    target_link_libraries(${PROJECT_NAME} PRIVATE licensecc::licensecc_static)
  endif()
endmacro()

# json
import_json()
# boost
import_boost()
# eigen
import_eigen3()
